import java.net.*;
import java.io.*;

public class Server extends Thread {

	private ServerSocket listener;
	private String filename;

	public Server(int port) throws IOException {
		listener = new ServerSocket(port);
		System.out.println("Server IP address: " + listener.getInetAddress());

	}
	public InetAddress getLocalAddr(){
		return listener.getInetAddress();
	}

	public void run() {
		System.out.println("for the sake of fuck sake");
//		
//		String testr2 = GUI1.myProject.testr;
//		System.out.println("testr: "+GUI1.myProject.testr);
//		GUI1.myProject.testr="B";
//		System.out.println("testr2: "+testr2);
//		System.out.println("testr: "+GUI1.myProject.testr);
//		
		while(GUI1.myProject.isRunning()) {
			System.out.println(this);
			Socket s = null;
			try {
				s = listener.accept();
			} catch (IOException e) {}
			if (s != null) {
				Peer curPeer =  new Peer(s.getInetAddress());
				Connection newConnection = new Connection(s);
				GUI1.myProject.activeConnections.putIfAbsent(curPeer, newConnection);
				newConnection.start();
				System.out.println("Active connections: "+ GUI1.myProject.activeConnections.keySet());
			}
		}
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	public String getFilename() {
		return filename;
	}

}

/*----------------------------
 code graveyard 
/*----------------------------

				//get the file name
				InputStreamReader is = new InputStreamReader(s.getInputStream());
				BufferedReader responses = new BufferedReader (is);
				String name = "";
				while (!responses.ready()){}
				while (responses.ready()) {
					String line =  responses.readLine();
					while(line!=null){
						name +=line;
					}
				}
filename =  name;

//look for the file and send it
 File file = new File (filename);
  byte [] bytesArray  = new byte [(int)file.length()];
  FileInputStream fis = new FileInputStream(file);
  BufferedInputStream bis = new BufferedInputStream(fis);
  bis.read(bytesArray,0,bytesArray.length);
  OutputStream os = s.getOutputStream();
  System.out.println("Sending the file...");
  os.write(bytesArray,0,bytesArray.length);
  os.flush();

*/
